
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言接口 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part21/" />
    
    
    <link rel="prev" href="../part19/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.21" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言接口</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x63A5;&#x53E3;">Go &#x8BED;&#x8A00;&#x63A5;&#x53E3;</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x63D0;&#x4F9B;&#x4E86;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x5373;&#x63A5;&#x53E3;&#xFF0C;&#x5B83;&#x628A;&#x6240;&#x6709;&#x7684;&#x5177;&#x6709;&#x5171;&#x6027;&#x7684;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x4EFB;&#x4F55;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x53EA;&#x8981;&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x63A5;&#x53E3; */</span>
<span class="hljs-keyword">type</span> interface_name <span class="hljs-keyword">interface</span> {
    method_name1 [return_type]
    method_name2 [return_type]
    method_name3 [return_type]
    ...
    method_namen [return_type]
}

<span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x7ED3;&#x6784;&#x4F53; */</span>
<span class="hljs-keyword">type</span> struct_name <span class="hljs-keyword">struct</span> {
    <span class="hljs-comment">/* variables */</span>
}

<span class="hljs-comment">/* &#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x65B9;&#x6CD5; */</span>
<span class="hljs-keyword">func</span> (struct_name_variable struct_name) method_name1() [return_type] {
    <span class="hljs-comment">/* &#x65B9;&#x6CD5;&#x5B9E;&#x73B0; */</span>
}
...
<span class="hljs-keyword">func</span> (struct_name_variable struct_name) method_namen() [return_type] {
    <span class="hljs-comment">/* &#x65B9;&#x6CD5;&#x5B9E;&#x73B0;*/</span>
}
</code></pre>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Phone <span class="hljs-keyword">interface</span> {
    call()
}

<span class="hljs-keyword">type</span> NokiaPhone <span class="hljs-keyword">struct</span> {

}

<span class="hljs-keyword">func</span> (nokiaPhone NokiaPhone) call() {
    fmt.Println(<span class="hljs-string">&quot;I am Nokia, I can call you!&quot;</span>)
}

<span class="hljs-keyword">type</span> IPhone <span class="hljs-keyword">struct</span> {

}

<span class="hljs-keyword">func</span> (iPhone IPhone) call() {
    fmt.Println(<span class="hljs-string">&quot;I am iPhone, I can call you!&quot;</span>)
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> phone Phone

    phone = <span class="hljs-built_in">new</span>(NokiaPhone)
    phone.call()

    phone = <span class="hljs-built_in">new</span>(IPhone)
    phone.call()
}
</code></pre>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;Phone&#xFF0C;&#x63A5;&#x53E3;&#x91CC;&#x9762;&#x6709;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;call()&#x3002;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x5728;main&#x51FD;&#x6570;&#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;Phone&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x5206;&#x522B;&#x4E3A;&#x4E4B;&#x8D4B;&#x503C;&#x4E3A;NokiaPhone&#x548C;IPhone&#x3002;&#x7136;&#x540E;&#x8C03;&#x7528;call()&#x65B9;&#x6CD5;&#xFF0C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sh">I am Nokia, I can call you!
I am iPhone, I can call you!
</code></pre>
<hr>
<h2 id="ps">PS</h2>
<ol>
<li><p>&#x63A5;&#x53E3;&#x4F20;&#x53C2;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> (
     <span class="hljs-string">&quot;fmt&quot;</span>
     <span class="hljs-string">&quot;strconv&quot;</span>
 )

 <span class="hljs-keyword">type</span> Phone <span class="hljs-keyword">interface</span> {
     call(param <span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span>
 }

 <span class="hljs-keyword">type</span> NokiaPhone <span class="hljs-keyword">struct</span> {

 }

 <span class="hljs-keyword">func</span> (nokiaPhone NokiaPhone) call(param <span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span> {
     ret := <span class="hljs-string">&quot;I am Nokia, I can call you! &quot;</span> + param
     fmt.Println(ret)
     <span class="hljs-keyword">return</span> ret
 }

 <span class="hljs-keyword">type</span> IPhone <span class="hljs-keyword">struct</span> {

 }

 <span class="hljs-keyword">func</span> (iPhone IPhone) call(param <span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span> {
     ret := <span class="hljs-string">&quot;I am iPhone, I can call you! &quot;</span> + param
     fmt.Println(ret)
     <span class="hljs-keyword">return</span> ret

 }

 <span class="hljs-keyword">func</span> main() {
     <span class="hljs-keyword">var</span> phone Phone

     phone = <span class="hljs-built_in">new</span>(NokiaPhone)
     phone.call(<span class="hljs-string">&quot;50&quot;</span>)

     phone = <span class="hljs-built_in">new</span>(IPhone)
     phone.call(<span class="hljs-string">&quot;100&quot;</span>)

     <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span>
     <span class="hljs-keyword">var</span> b <span class="hljs-keyword">string</span>
     a = <span class="hljs-number">1</span>
     <span class="hljs-comment">// int -&gt; string</span>
     b = strconv.Itoa(a)
     <span class="hljs-comment">// string -&gt; int</span>
     a, _ = strconv.Atoi(b)
     fmt.Println(a)
     fmt.Println(b)
 }
</code></pre>
</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part19/" class="navigation navigation-prev " aria-label="Previous page: Go 语言类型转换">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part21/" class="navigation navigation-next " aria-label="Next page: Go 错误处理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言接口","level":"1.21","depth":1,"next":{"title":"Go 错误处理","level":"1.22","depth":1,"path":"part21/README.md","ref":"part21/README.md","articles":[]},"previous":{"title":"Go 语言类型转换","level":"1.20","depth":1,"path":"part19/README.md","ref":"part19/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part20/README.md","mtime":"2021-01-20T07:58:49.122Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

